package com.csnf.mall.controllermvc;

import com.csnf.mall.entity.Category;
import com.csnf.mall.entity.Product;
import com.csnf.mall.mapper.CategoryMapper;
import com.csnf.mall.servermvc.IAdminProductServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminProductController {

    //    @Controller  用于标记这个类 为 一个servlet
//    @RequestMapping 用于标记一个方法 并给其设置一个 url 地址
//    @ResponseBody 用于指定这个方法 返回形式 是json 字符串
    @Autowired
    IAdminProductServer adminProductServer;
    @Autowired
    CategoryMapper categoryMapper;

    @RequestMapping("/admin/product")
    public String productTest() {
        // 返回的是 重定向到 homepage.jsp 页面
        return "homepage";
    }

    @RequestMapping("/admin/tjson")
    @ResponseBody
    public String testJson() {
        return "test";
    }

    @RequestMapping("/admin/getProducts")
//    @ResponseBody
    public ModelAndView getAllProduct(ModelAndView modelAndView) {
        List<Product> products = adminProductServer.getAllProducts();
        List<Category> categories = categoryMapper.getOnlyCategory();
//        modelAndView.addObject(products);
//        modelAndView.addObject(categories);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("productList", products);
        map.put("categories", categories);
        modelAndView.addAllObjects(map);
        modelAndView.setViewName("productpage");
        return modelAndView;
    }

    //    @RequestParam  获取 url中的请求参数
    @DeleteMapping("/admin/product")
    @ResponseBody
    public String deleteProduct(@RequestParam("productId") Integer productId) {
        return "调用成功" + productId;
    }

    @PostMapping("/admin/product/search")
    public ModelAndView getProductByParam(
            @RequestParam("productName") String productName,
            @RequestParam("categoryId") Integer categoryId,
            ModelAndView modelAndView) {
        List<Product> products =
                adminProductServer.getProductByParam(productName, categoryId);
        List<Category> categories = categoryMapper.getOnlyCategory();
//        modelAndView.addObject(products);
//        modelAndView.addObject(categories);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("productList", products);
        map.put("categories", categories);
        modelAndView.addAllObjects(map);
        modelAndView.setViewName("productpage");
        return modelAndView;
    }


    @RequestMapping("/admin/product/detail")
    public ModelAndView getProductDetail(
            @RequestParam("productId")Integer productId,ModelAndView modelAndView) {
        Product product = adminProductServer.getProductDetail(productId);
        modelAndView.addObject("product", product);
        modelAndView.setViewName("productdetail");
        return modelAndView;
    }
}
